;
(function() {
    // var root = 'http://192.168.19.49:9898/api/v1.0/';
    switch (window.location.host) {
        case 'art2020.cafa.edu.cn':
            root = '//art2020.cafa.edu.cn/api/v1.0/';
            break;
        case 'cafadev.test':
            root = '//cafadev.test/api/v1.0/';
            break;
        case 'dev.cafa720.cn':
            root = '//dev.cafa720.cn/api/v1.0/';
            break;
        default:
            root = '//192.168.19.49:9898/api/v1.0/';
            break;
    }
    var GET_STUDENT_WORK_LIST = root + 'student/works/list';
    var GET_WORK_DETAIL = root + 'student/works/detail';
    var GET_STUDENT_INFO = root + 'student/info';
    var UPDATE_WORK_VIEW = root + 'student/works/view';
    var HANDLE_WORK_LIKE = root + 'student/works/good';
    var GET_SHOWROOM_INFO = root + 'showroom/info';
    var GET_SEARCH_LIST = root + 'student/search2';
    var GET_COLLEGE_LIST = root + 'college/list';
    var GET_COLLEGE_INFO = root + 'college/info';
    var GET_COLLEGE_DIRECTION = root + 'college/direction';
    var GET_PAGE_WORK_LIST = root + 'student/works/get_page_list';
    var GET_VIDEO_DETAIL = root + 'showroom/video_detail';
    var GET_STUDENT_VIDEO_DETAIL = root + 'student/video_detail';
    var GET_SHOWROOM_LIST = root + 'showroom/get_list';
    var GET_WEIXIN_SHARE_CONFIG = root + 'share/weixin_config';

    var ajax = {};
    /**
     * 获取学生作品列表
     * @param options object 参数对象
     */
    ajax.getWorkList = function(options) {
        var defaultOptions = {
            student_no: null,
            success_cb: null,
            fail_cb: null
        }
        var opts = $.extend(defaultOptions, options);
        if (!opts.student_no) {
            return;
        }
        $.ajax({
            url: GET_STUDENT_WORK_LIST,
            type: 'post',
            data: JSON.stringify({
                student_no: opts.student_no
            }),
            success: function(res) {
                if (res.errorcode) {
                    alert(res.message);
                    return;
                }
                if (opts.success_cb && typeof opts.success_cb === 'function') {
                    opts.success_cb(res);
                }
            },
            fail: function(err) {
                if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                    opts.fail_cb(err);
                }
            }
        })
    }

    /**
     * 获取学生作品列表
     * @param options object 参数对象
     */
    ajax.getWorkDetail = function(options) {
        var defaultOptions = {
            id: null,
            success_cb: null,
            fail_cb: null
        }
        var opts = $.extend(defaultOptions, options);
        if (!opts.id) {
            return;
        }
        $.ajax({
            url: GET_WORK_DETAIL,
            type: 'post',
            data: JSON.stringify({
                id: opts.id
            }),
            success: function(res) {
                if (res.errorcode) {
                    alert(res.message);
                    return;
                }
                if (opts.success_cb && typeof opts.success_cb === 'function') {
                    opts.success_cb(res);
                }
            },
            fail: function(err) {
                if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                    opts.fail_cb(err);
                }
            }
        })
    }

    /**
     * 获取学生信息
     * @param options object 参数对象
     */
    ajax.getStudentInfo = function(options) {
        var defaultOptions = {
            student_no: null,
            success_cb: null,
            fail_cb: null
        }
        var opts = $.extend(defaultOptions, options);
        if (!opts.student_no) {
            return;
        }
        $.ajax({
            url: GET_STUDENT_INFO,
            type: 'post',
            dataType: 'json',
            data: JSON.stringify({
                student_no: opts.student_no
            }),
            success: function(res) {
                if (res.errorcode) {
                    alert(res.message);
                    return;
                }
                if (opts.success_cb && typeof opts.success_cb === 'function') {
                    opts.success_cb(res);
                }
            },
            fail: function(err) {
                if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                    opts.fail_cb(err);
                }
            }
        })
    }

    /**
     * 更新作品浏览数
     * @param options object 参数对象
     */
    ajax.updateWorkView = function(options) {
        var defaultOptions = {
            work_id: null,
            success_cb: null,
            fail_cb: null
        }
        var opts = $.extend(defaultOptions, options);
        if (!opts.work_id) {
            return;
        }

        $.ajax({
            url: UPDATE_WORK_VIEW,
            type: 'post',
            dataType: 'json',
            data: JSON.stringify({
                work_id: opts.work_id
            }),
            success: function(res) {
                if (res.errorcode) {
                    alert(res.message);
                    return;
                }
                if (opts.success_cb && typeof opts.success_cb === 'function') {
                    opts.success_cb(res);
                }
            },
            fail: function(err) {
                if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                    opts.fail_cb(err);
                }
            }
        })
    }

    /**
     * 作品点赞
     * @param options object 参数对象
     */
    ajax.handleWorkLike = function(options) {
        var defaultOptions = {
            work_id: null,
            success_cb: null,
            fail_cb: null
        }
        var opts = $.extend(defaultOptions, options);
        if (!opts.work_id) {
            return;
        }

        $.ajax({
            url: HANDLE_WORK_LIKE,
            type: 'post',
            dataType: 'json',
            data: JSON.stringify({
                work_id: opts.work_id
            }),
            success: function(res) {
                if (res.errorcode) {
                    alert(res.message);
                    return;
                }
                if (opts.success_cb && typeof opts.success_cb === 'function') {
                    opts.success_cb(res);
                }
            },
            fail: function(err) {
                if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                    opts.fail_cb(err);
                }
            }
        })
    }

    /**
     * 获取全景配置
     * @param options object 参数对象
     */
    ajax.getShowroomInfo = function(options) {
        var defaultOptions = {
            guid: null,
            success_cb: null,
            fail_cb: null
        }
        var opts = $.extend(defaultOptions, options);
        if (!opts.guid) {
            return;
        }

        $.ajax({
            url: GET_SHOWROOM_INFO,
            type: 'post',
            dataType: 'json',
            data: JSON.stringify({
                guid: opts.guid
            }),
            success: function(res) {
                if (res.errorcode) {
                    alert(res.message);
                    return;
                }
                if (opts.success_cb && typeof opts.success_cb === 'function') {
                    opts.success_cb(res);
                }
            },
            fail: function(err) {
                if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                    opts.fail_cb(err);
                }
            }
        })
    }

    /**
     * 作品点赞
     * @param options object 参数对象
     */
    ajax.getSearchInfo = function(options) {
        var defaultOptions = {
            keyword: null,
            type: null,
            page: null,
            success_cb: null,
            fail_cb: null
        }
        var opts = $.extend(defaultOptions, options);
        if (!opts.keyword) {
            return;
        }

        $.ajax({
            url: GET_SEARCH_LIST,
            type: 'post',
            dataType: 'json',
            data: JSON.stringify({
                keyword: opts.keyword,
                type: opts.type,
                page: opts.page
            }),
            success: function(res) {
                if (res.errorcode) {
                    alert(res.message);
                    return;
                }
                if (opts.success_cb && typeof opts.success_cb === 'function') {
                    opts.success_cb(res);
                }
            },
            fail: function(err) {
                if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                    opts.fail_cb(err);
                }
            }
        })
    }

    /**
     * 获取学院列表
     * @param options object 参数对象
     */
    ajax.getCollegeList = function(options) {
        var defaultOptions = {
            type: null,
            success_cb: null,
            fail_cb: null
        }
        var opts = $.extend(defaultOptions, options);
        if (!opts.type) {
            return;
        }
        $.ajax({
            url: GET_COLLEGE_LIST,
            type: 'post',
            dataType: 'json',
            data: JSON.stringify({
                type: opts.type
            }),
            success: function(res) {
                if (res.errorcode) {
                    alert(res.message);
                    return;
                }
                if (opts.success_cb && typeof opts.success_cb === 'function') {
                    opts.success_cb(res);
                }
            },
            fail: function(err) {
                if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                    opts.fail_cb(err);
                }
            }
        })
    }
    ajax.getPageWorkList = function(options) {
            var defaultOptions = {
                college_no: null,
                type: null,
                page: null,
                success_cb: null,
                fail_cb: null
            }
            var opts = $.extend(defaultOptions, options);
            if (!opts.college_no) {
                return;
            }
            var data = {
                college_no: opts.college_no,
                type: opts.type,
                page: opts.page,
            };
            if (options.department_no) {
                data.department_no = options.department_no;
            }
            $.ajax({
                url: GET_PAGE_WORK_LIST,
                type: 'post',
                data: JSON.stringify(data),
                success: function(res) {
                    if (res.errorcode) {
                        alert(res.message);
                        return;
                    }
                    if (opts.success_cb && typeof opts.success_cb === 'function') {
                        opts.success_cb(res);
                    }
                },
                fail: function(err) {
                    if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                        opts.fail_cb(err);
                    }
                }
            })
        },
        /**
         * 获取视频详情
         */
        ajax.getVideoDetail = function(options) {
            var defaultOptions = {
                video_id: null,
                success_cb: null,
                fail_cb: null
            }
            var opts = $.extend(defaultOptions, options);
            if (!opts.video_id) {
                return;
            }
            var data = {
                video_id: opts.video_id,
            };
            $.ajax({
                url: GET_VIDEO_DETAIL,
                type: 'post',
                data: JSON.stringify(data),
                success: function(res) {
                    if (res.errorcode) {
                        alert(res.message);
                        return;
                    }
                    if (opts.success_cb && typeof opts.success_cb === 'function') {
                        opts.success_cb(res);
                    }
                },
                fail: function(err) {
                    if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                        opts.fail_cb(err);
                    }
                }
            })
        }
        /**
         * 获取学生视频详情
         */
    ajax.getStudentVideoDetail = function(options) {
        var defaultOptions = {
            student_no: null,
            video_id: null,
            success_cb: null,
            fail_cb: null
        }
        var opts = $.extend(defaultOptions, options);
        if (!opts.student_no || !opts.video_id) {
            return;
        }
        var data = {
            student_no: opts.student_no,
            video_id: opts.video_id
        };
        $.ajax({
            url: GET_STUDENT_VIDEO_DETAIL,
            type: 'post',
            data: JSON.stringify(data),
            success: function(res) {
                if (res.errorcode) {
                    alert(res.message);
                    return;
                }
                if (opts.success_cb && typeof opts.success_cb === 'function') {
                    opts.success_cb(res);
                }
            },
            fail: function(err) {
                if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                    opts.fail_cb(err);
                }
            }
        })
    }

    /**
     * 获取学院信息
     */
    ajax.getCollegeInfo = function(options) {
            var defaultOptions = {
                college_no: null,
                success_cb: null,
                fail_cb: null
            }
            var opts = $.extend(defaultOptions, options);
            if (!opts.college_no) {
                return;
            }
            var data = {
                college_no: opts.college_no,
            };
            $.ajax({
                url: GET_COLLEGE_INFO,
                type: 'post',
                data: JSON.stringify(data),
                success: function(res) {
                    if (res.errorcode) {
                        alert(res.message);
                        return;
                    }
                    if (opts.success_cb && typeof opts.success_cb === 'function') {
                        opts.success_cb(res);
                    }
                },
                fail: function(err) {
                    if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                        opts.fail_cb(err);
                    }
                }
            })
        }
        /**
         * 获取展厅列表
         */
    ajax.getShowroomList = function(opts) {
            $.ajax({
                url: GET_SHOWROOM_LIST,
                type: 'post',
                data: '',
                success: function(res) {
                    if (res.errorcode) {
                        alert(res.message);
                        return;
                    }
                    if (opts.success_cb && typeof opts.success_cb === 'function') {
                        opts.success_cb(res);
                    }
                },
                fail: function(err) {
                    if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                        opts.fail_cb(err);
                    }
                }
            })
        },
        /**
         * 获取微信分享配置信息
         */
        ajax.getWeixinShareConfig = function(options) {
            var defaultOptions = {
                url: null,
                success_cb: null,
                fail_cb: null
            }
            var opts = $.extend(defaultOptions, options);
            if (!opts.url) {
                return;
            }
            var data = {
                url: opts.url,
            };
            $.ajax({
                url: GET_WEIXIN_SHARE_CONFIG,
                type: 'post',
                data: JSON.stringify(data),
                success: function(res) {
                    if (res.errorcode) {
                        alert(res.message);
                        return;
                    }
                    if (opts.success_cb && typeof opts.success_cb === 'function') {
                        opts.success_cb(res);
                    }
                },
                fail: function(err) {
                    if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                        opts.fail_cb(err);
                    }
                }
            })
        },
    /**
     * 获取学院研究方向
     */
    ajax.getDirectionInfo = function(options) {
        var defaultOptions = {
            direction_id: null
        }
        var opts = $.extend(defaultOptions, options);
        if (!opts.direction_id) {
            return;
        }
        var data = {
            direction_id: opts.direction_id,
        };
        $.ajax({
            url: GET_COLLEGE_DIRECTION,
            type: 'post',
            data: JSON.stringify(data),
            success: function(res) {
                if (res.errorcode) {
                    alert(res.message);
                    return;
                }
                if (opts.success_cb && typeof opts.success_cb === 'function') {
                    opts.success_cb(res);
                }
            },
            fail: function(err) {
                if (opts.fail_cb && typeof opts.fail_cb === 'function') {
                    opts.fail_cb(err);
                }
            }
        })
    }
        window.ajax = ajax;
})();